package com.demo.java.controller;

import com.demo.java.model.entity.Person;
import com.demo.java.model.params.Query;
import com.demo.java.model.params.Res;
import com.demo.java.service.RedisCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/api/v1/redis")
public class RedisCacheController {

    @Autowired
    private RedisCacheService redisCacheService;

    @RequestMapping(value = "/get", method = {RequestMethod.GET, RequestMethod.POST})
    public Object get(@RequestBody Query query) {
        Person person = redisCacheService.getCache(query);
        if (Objects.isNull(person)) {
            return Res.fail();
        }
        return Res.ok(person);
    }

    @RequestMapping(value = "update", method = {RequestMethod.GET, RequestMethod.POST})
    public Object update(@RequestBody Query query) {
        if (redisCacheService.update(query)) {
            return Res.ok();
        }
        return Res.fail();
    }
}
